﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLC
{
    class Compiler
    {
        public HashSet<char> SetOperator = new HashSet<char>()
        {
            '(',
            ')',
            '[',
            ']',
            '{',
            '}',
            ':',
            ';',
            ',',
            '+',
            '-',
            '*',
            '/',
            '%',
            '.',
            '\t',
            '\n',
            '=',
            '<',
            '>',
            '!',
            '~',
            '?',
            ' '
        };

        public HashSet<char> SetChar = new HashSet<char>()
        {
            '&',
            '$',
            '@'
        };

        public string Compile(string code)
        {
            List<string> results = new List<string>();
            string letter = "";
            for (int i = 0; i < code.Length; i++)
            {
                if (code[i] == '\"')
                {
                    for (int j = i + 1; j < code.Length; j++)
                    {
                        if (code[j] == '\"')
                        {
                            letter = code.Substring(i, j - i + 1);
                            results.Add(letter);
                            i = j;
                            break;
                        }
                    }
                }
                else if (SetOperator.Contains(code[i]))
                {
                    results.Add(code[i].ToString());
                }
                else if (SetChar.Contains(code[i]))
                {
                    for (int j = i + 1; j < code.Length; j++)
                    {
                        if (SetOperator.Contains(code[j]))
                        {
                            letter = code.Substring(i, j - i);

                            int count = letter.Count(item => item == code[i]);
                            if (count == 2)
                            {
                                switch (code[i])
                                {
                                    case '&':
                                        if (i == 0)
                                        {
                                            letter = $"struct{letter}";
                                        }
                                        break;
                                    case '$':
                                        if (i == 0)
                                        {
                                            letter = $"static{letter}";
                                        }
                                        else if (code[i - 1] == '\t')
                                        {
                                            letter = $"public{letter}";
                                        }
                                        break;
                                    case '@':
                                        if (i == 0)
                                        {
                                            letter = $"static{letter}";
                                        }
                                        else if (code[i - 1] == '\t')
                                        {
                                            letter = $"public{letter}";
                                        }
                                        break;
                                }
                            }

                            letter = letter.Replace(code[i], ' ').Trim();
                            i = j - 1;
                            results.Add(letter);
                            break;
                        }
                    }
                }
                else
                {
                    results.Add(code[i].ToString());
                }
            }
            return string.Join("", results);
        }
    }
}
